﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tracy.DataModel;
using TracyGUI.UIComponent;
using Tracy;
using Tracy.Messages;
using Tracy.Handlers;
using SharedLib.MessageFramework;
using Tracy.DataModel.UltraExplorer;
using SharedLib.XmlDataModel;
using SharedLib.DataAccess;

namespace TracyGUI
{
    public partial class FormMain
    {
        TreeNode _selectedUETreeNode = null;
        UltraExplorer<Entry> _ueEntry;
        private void LoadEntryUltraExplorer()
        {
            //Init UE
            _ueEntry = new UltraExplorer<Entry>(treeViewEntryUE, "All Entries");
            //_ueEntry.CurrentClassifierSet = TracyBus.Instance.EntryClassifierProvider.ClassifierSets["Default"];
            
            ReloadEntriesToList(_ueEntry.GetAllItems());
            //_ueEntry.ReloadTree();

            //Load classifier sets to dropdown list
            foreach (string classifierSet in TracyFacade.Instance.EntryClassifierProvider.ClassifierSets.Keys)
            {
                comboBoxUE.Items.Add(classifierSet);
            }
            if (comboBoxUE.Items.Count > 0) comboBoxUE.SelectedIndex = 0;

            //Adjust splitContainer layout
            splitContainerUE.SplitterDistance = 21;
        }

        private void treeViewEntryUE_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Action != TreeViewAction.Unknown)
            {
                SelectUENode(e.Node);
            }
        }

        private void SelectUENode(TreeNode node)
        {
            ReloadEntriesToList(_ueEntry.GetMatchedItems(node));
            _selectedUETreeNode = node;
        }

        private void treeViewEntryUE_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            if (_ueEntry.IsLastGeneratedNode(e.Node))
            {
                e.Node.Nodes.Clear();
                _ueEntry.GenerateFilterChildNodes(e.Node);
            }
        }

        private void comboBoxUE_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxUE.SelectedIndex >= 0)
            {
                if (_ueEntry.IsLastGeneratedNode(_ueEntry.RootNode))
                {
                    return;
                }
                if (comboBoxUE.SelectedIndex >= 0)
                {
                    _ueEntry.CurrentClassifierSet = TracyFacade.Instance.EntryClassifierProvider.ClassifierSets[comboBoxUE.SelectedItem.ToString()];
                }
                else
                {
                    _ueEntry.CurrentClassifierSet = new List<string>();
                }

                _ueEntry.ReloadTree();
            }
        }
    }
}